﻿using System.Collections.ObjectModel;
using Newtonsoft.Json;

namespace EduAssess.Models;

public class Question
{
    [JsonIgnore] public Guid Id { get; set; }
    public string Name { get; set; }
    public ObservableCollection<Answer> Answers { get; set; }

    public Question()
    {
        Id = Guid.NewGuid();
        Answers = new ObservableCollection<Answer>();
    }

    public bool CheckAnswer(Question question)
        => !Answers.Where((t, i) => question.Answers[i].IsTrue != t.IsTrue).Any();

    public Answer GetRightAnswer()
    {
        var answer = Answers.FirstOrDefault(item => item.IsTrue)!;

        return answer;
    }

    public bool IsCorrectQuestion()
    {
        if (string.IsNullOrWhiteSpace(Name)) return false;

        if (Answers.Count == 0) return false;

        foreach (var item in Answers)
        {
            if (!item.IsCorrectAnswer()) return false;

            if (item.IsTrue) return true;
        }

        return false;
    }
}
